// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var nature = $("#nature").val();
	var enterpriseId;
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
	var router = require("../hash/pageRouter");
	var css_Eqianbao = require("./client_Eqianbao.css");
    
    var init = function(page){
		enterpriseId = page.params.enterpriseId;
		$(".nav_active").removeClass("nav_active1");
		$(".authenticationState").addClass("nav_active1");
		inquireEnterpriseDetail(enterpriseId);
		router.init(); 
		initEvent();
    };
    
	var initEvent = function(){
		
		$("#client_Eqianbao").on("change", "#creditCard", function(){
			var bn = tools.getBankName($(this).val());
			if (bn == ""){
				alert("未知开户行！");
			} else {
				$("#depositBank").val(bn);
			}
		});
		
		$("#client_Eqianbao").on("change", "#legalIdcard", function(){
			var promptString = validate.isIdCard($(this), "请输入正确的身份证号码!");
			if ("" != promptString) {
				base.alert(2, promptString)
			} 
		});
		
		/*layer.tips(tools.formatCurrency($("input[name=registeredCapital]").val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
		  tips: [1, '#3595CC'],
		  time: 3000
		});*/
		
		$("#client_Eqianbao").on("focus", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$("#client_Eqianbao").on("change", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		//认证提交
		$("#client_Eqianbao").on("click", ".next", function(){
			databind.submitDom($("#client_Eqianbao"), function(){
				history.go(-1);
		  	});
		});
		
//		单位类型 nature 
//		企业名 enterpriseName 
//		单位注册号 regNo 
//		营业执照所在地 province
//		联系地址 contactAddress
//		经营范围 businessScope
//		注册资金 registeredCapital
//		营业期限 businessTimeLimit
//		法定代表人姓名 legalPerson
//		身份证号 legalIdcard
//		开户行 depositBank
//		开户名称 subbranch
//		银行卡号 creditCard

		/*databind.fillInput1($("#client_Eqianbao"), {action: "inquireEnterpriseDetail.json"}, function(data){
			$("select[name=city]").html("<option>"+data.datas[0].city+"</option>");
			$("select[name=district]").html("<option>"+data.datas[0].district+"</option>");
		});*/
		
		//点击企业基本信息
		$(".clientManage_details1 .enterpriseInfo").on("click", function() {
			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
		});

		$(".financeSituation").on("click", function() {
			router.route("client/clientManage_financeSituation.html?enterpriseId=" + enterpriseId);
		});

		//证明材料
		$(".financeEvidentiary").on("click", function() {
			router.route("client/clientManage_financeEvidentiary.html?enterpriseId=" + enterpriseId);
		});
		
		//点击企业基本信息
		$(".enterpriseInfo").on("click", function() {
			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
		});
		
		//我的客户
		$(".financeBelongs").on("click", function() {
			router.route("client/clientManage_belongs.html?enterpriseId=" + enterpriseId);
		});
		
		//实名认证
		$(".authenticationState").on("click", function() {
			router.route("client/clientManage_authentication.html?enterpriseId=" + enterpriseId);
		});
    };
    
  //查询企业详细信息
	var inquireEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireEnterpriseDetail.json",
			oData: {
				enterpriseId: enterpriseId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
						
					/*if (data.datas[0].enterpriseGradeEvaluationDetail != null) {
						
					}*/
					databind.fillForm($("#client_Eqianbao"), data.datas[0]);
					$("select[name=province]").html("<option>"+data.datas[0].province+"</option>");
					$("select[name=city]").html("<option>"+data.datas[0].city+"</option>");
			        $("select[name=district]").html("<option>"+data.datas[0].district+"</option>");
				} else {
					base.alert(2, data.message);
				}
				//判断认证状态
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication != 3) { //未认证
						$(".return").hide();
					}
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication == 3) { //已认证
						$(".next").hide();
					}
				//调显示与隐藏函数
				hideSpan();
		        hideCardType();
			}
		});
		databind.disableInput("#client_Eqianbao");
		databind.enableInput("#client_Eqianbao .bankInfo");
	};
    
	//联动1
	var hideSpan = function() {
		var sUserType = $("#sUserType").val();
		if(sUserType == "以代理人身份注册") {
			$("#hide1").show();
			$("#hide2").show();
		}
		if(sUserType == "以法定代表人身份注册") {
			$("#hide2").hide();
			$("#hide1").show();
		}
	};
	
	//联动2
	var hideCardType = function() {
		var scardType = $("#scardType").val();
		if(scardType == "0") {
			$("#hideRegNo").show();
			$("#certificateShow").show();
			$("#codeUSCHide").hide();
		}
		if(scardType == "1") {
			$("#hideRegNo").hide();
			$("#certificateShow").hide();
			$("#codeUSCHide").show();
		}
	};
	
	//连接e签宝生成证书,返回账户唯一标识
    var addOrganize = function(){
		base.fnAjax({
			action: "addOrganize.json",
			async: false,
			oData: {
            }, //传递参数
            fnSuccess: function(data) {
            }
		});
	};
	
    return {
        init:init
    };
});